package com.xinyue.game.fish.service;

import java.util.Optional;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.mygame.dao.PlayerDao;
import com.mygame.db.entity.Player;
import com.mygame.db.entity.model.PlayerBasic;
import com.xinyue.game.fish.common.EnumRedisKey;
@Service
public class PlayerService {

	@Autowired
	private PlayerDao playerDao;
	@Autowired
	private StringRedisTemplate redisTemplate;

	public void updatePlayerBasic(Player player) {
		PlayerBasic playerBasic = new PlayerBasic();
		BeanUtils.copyProperties(player, playerBasic);
		String value = JSON.toJSONString(playerBasic);
		String key = EnumRedisKey.PlayerBasic.getRedisKey(player.getPlayerId());
		redisTemplate.opsForValue().set(key, value);
	}
	
	public Optional<Player> getPlayer(long playerId){
		return  playerDao.findByIdFromCacheOrLoader(playerId);
	}
	public void updatePlayer(Player player) {
		playerDao.saveOrUpdate(player, player.getPlayerId());
	}
	
	public PlayerBasic getPlayerBasic(long playerId) {
		String key = EnumRedisKey.PlayerBasic.getRedisKey(playerId);
		String value = redisTemplate.opsForValue().get(key);
		if(value != null) {
			return JSON.parseObject(value, PlayerBasic.class);
		}
		return null;
	}
}
